*** REPLICATION TOOLKIT ***

* AUTHORS: CIMINELLI DUVAL FURCERI 

* ARTICLE: EMPLOYMENT PROTECTION DEREGULATION AND LABOR SHARES IN ADVANCED ECONOMIES 

* JOURNAL: REVIEW OF ECONOMICS AND STATISTICS

* OUTPUT: FIGURE E4 (ONLINE APPENDIX)

***************************

cd ""

use "DTA files/Dataset", clear

**************** WITHIN/BETWEEN INDUSTRY DECOMPOSITION *******************

* Calculate industry share of total VA by country
gen a = 100*lab/va
capture drop lab_country va_country a_country
egen lab_country = total(lab) if a!=., by(year country) 
egen va_country = total(va) if a!=., by(year country) 
gen a_country = 100*lab_country/va_country
gen va_share = 100*va/va_country


* Calculate mean of indsutry share of total VA
egen share_mean = mean(va_share) if va_share!=., by(country industry)

* Trends for 1970-2010 period
egen trend = group(year)

* Create matrix where to store intermediate calculations (trends1, shares, within_industry, within1), and linear trends by country (trends) 
egen id1=group(ifs)
quietly sum id1
local rows = r(max)
mat trends = J(`rows',1,.)

matrix rownames trends = UnitedStates UnitedKingdom Austria Belgium Denmark France Germany Italy Luxembourg Netherlands Sweden Canada Japan Finland Greece Ireland Portugal Spain Australia Korea CzechRepublic Slovakia

egen id2=group(industry)
quietly sum id2
local cols = r(max)

mat within1 = J(`cols',1,1)
mat trends1 = J(`rows',`cols',0) 
mat shares = J(`rows',`cols',0) 
mat within_industry = J(`rows',`cols',.)

foreach x in shares trends1 within_industry {
matrix colnames `x' = FoodBeveragesTobacco Textiles Wood&Paper Coke&Petroleum Chemicals Rubber&MineralProducts BasicMetals Electrical&Optical Machinery&Equipment TransportEquipment OtherManufacturing  Wholesale&RetailMotor WholesaleNon-Motor RetailNon-Motor Transport&Storage Postal&Courier Publishing&Audiovisual Telecommunications IT&Others Agriculture&Forestry&Fisheries Mining&Quarrying Utilities Construction Accomodation&FoodServices Financial&Insurance RealEstate Professional&SupportActivities PA&Defense&SS Education Health&Social Arts&Recreation OtherServiceActivities
}


* Perform regression and save relevant results
forvalues i=1/`rows' {
xtset id_cross_section year
quietly xtreg a_country trend if id1==`i' & id2==1, fe

* Fill in trends2 matrix
matrix results = r(table)
matrix trends[`i',1] = results[1,1]

* Estimate linear trends in labor share by industry in each country 
forvalues j=1/`cols' {
quietly sum share_mean if id1==`i' & id2==`j'
local sc1 = r(N)
quietly sum a if id1==`i' & id2==`j'
local sc2 = r(N)
if `sc1'!=0 & `sc2'!=0 {
quietly regress a trend if id1==`i' & id2==`j'
* Fill in trends matrix
matrix results = r(table)
matrix trends1[`i',`j'] = results[1,1]
* Fill in shares matrix
quietly sum share_mean if id1==`i' & id2==`j', meanonly
local sc1 = r(mean)
matrix shares[`i',`j'] = `sc1'
}

* Compute within industry changes 
matrix within_industry[`i',`j'] = trends1[`i',`j']*shares[`i',`j']  
}
}
* Compute sum of within industry changes by country
matrix within = within_industry*within1
matrix rownames within = UnitedStates UnitedKingdom Austria Belgium Denmark France Germany Italy Luxembourg Netherlands Sweden Canada Japan Finland Greece Ireland Portugal Spain Australia Korea CzechRepublic Slovakia

* Transform matrices within and trends in variables to plot, create variables with country names  
matrix trans_trends = trends'
matrix trans_within = within'
matrix rowtest1 = trans_trends["c1", 1...]
matrix rowtest2 = trans_within["c1", 1...]
local names : colnames rowtest1
local n_names : word count `names'
capture drop var_colnames
gen var_colnames = ""
forval i=1/`n_names' {
local this_name = "`:word `i' of `names''"
replace var_colnames = "`this_name'" if _n == `i'
}
matrix trans_rowtest1 = rowtest1'
matrix trans_rowtest2 = rowtest2'
svmat trans_rowtest1, name(Trends)
svmat trans_rowtest2, name(Within)

* Create scatter plot of within component over linear trend
gen Within=Within1/100

* Regress Within component on trends and save regression results
regress Trends1 Within
local sc1 = round(100*e(r2),1)
local sc4 = `sc1'/100
matrix results = r(table)
local sc2 = round(results[1,2],0.01)
local sc3 = round(results[1,1],0.01)


sum Trends1 
local min = r(min) - r(min)/5
sum Within 
local max = r(max) - r(max)/2

* Adds significance
local pval = results[4,1]
if `pval'>0.1 { 
local star = ""
}
if `pval'<=0.1 { 
local star = "*"
}
if `pval'<=0.05 {
local star = "**"
}
if `pval'<=0.001 {
local star = "***"
}
display `text'
graph twoway scatter Trends1 Within, mlabel(var_colnames) || lfit Trends1 Within, ////
ytitle("Country labor share trends") xtitle("Within industry component") ylabel(-0.5 (0.5) 1) xlabel(-0.5 (0.5) 1)  yline(0, lcolor(black)) ///
graphregion(color(white)) legend(off) text(`min' `max' "{&beta}`star'=`sc3',   R^2=`sc4'")
graph export "Figures/E4.png", replace 
